package band.wukong.practice.javase.core.thread;


import java.util.Vector;

/**
 * Desc:
 * Author: sunjs
 * Date: 2009-11-5 9:13:45
 */
public abstract class ThreadProcessor implements Runnable {

    public static final boolean DEFAULT_DAEMON_THREAD = true;  //守护线程
    public static final boolean NON_DAEMON_THREAD = false;  //非守护线程

    private volatile boolean pause;
    private Vector buf;
    private int threadCount = 1;  //默认一个线程

    private boolean daemon = DEFAULT_DAEMON_THREAD;  //默认为守护线程

    private String threadName;  //线程名称
    private int sleepTime = 0;  //睡眠时间

    public ThreadProcessor() {
        this.init();
    }

    public ThreadProcessor(int count) {
        this.threadCount = count;
        this.init();
    }

    public ThreadProcessor(int count, boolean isDaemon) {
        this.threadCount = count;
        this.daemon = isDaemon;
        this.init();
    }

    public ThreadProcessor(int count, boolean isDaemon, int sleepTime) {
        this.threadCount = count;
        this.daemon = isDaemon;
        this.sleepTime = sleepTime;
        this.init();
    }

    public ThreadProcessor(int count, boolean isDaemon, int sleepTime, String threadName) {
        this.threadCount = count;
        this.daemon = isDaemon;
        this.sleepTime = sleepTime;
        this.threadName = threadName;
        this.init();
    }

    private void init() {
        this.pause = false;
        this.buf = new Vector();
        Thread thread = null;
        for (int i = 1; i <= threadCount; i++) {
            thread = new Thread(this);
            thread.setDaemon(this.daemon);
            if (this.threadName != null && !"".equals(this.threadName.trim())) {
                thread.setName(this.threadName + i);
            }
            thread.start();
        }
    }

    public void run() {
        while (true) {
            while (this.buf.size() > 0) {
                try {
                    this.process(this.buf.remove(0));
                    if (this.sleepTime > 0) {
                        Thread.sleep(this.sleepTime);
                    }
                } catch (Exception ex) {
                    System.out.println(Thread.currentThread().getName() + " : error when running!");
                }
            }
            this.pause = true;
            if (this.pause) {
                synchronized (this) {
                    while (this.pause) {
                        try {
                            System.out.println(Thread.currentThread().getName() + " : begin to wait!");
                            wait();
                        } catch (InterruptedException ex) {
                            System.out.println(Thread.currentThread().getName() + " : error when waitting!");
                        }
                    }
                }
            }
        }
    }

    public synchronized void queue(Object obj) {
        this.buf.add(obj);
        if (this.pause) {
            this.pause = false;
            System.out.println(Thread.currentThread().getName() + " : begin to notifyAll Thread");
            notifyAll();
        }
    }

    /**
     * 线程的具体处理方法，需要子类实现
     *
     * @param obj
     * @throws Exception
     */
    public abstract void process(Object obj) throws Exception;

    public int getBufferCount() {
        return this.buf.size();
    }

    public int getThreadCount() {
        return threadCount;
    }

    public String getThreadName() {
        return threadName;
    }

    public int getSleepTime() {
        return sleepTime;
    }
}

